Ext.onReady(function(){
	ORG_DISK.initPage();
});
var ORG_DISK={
		initPage: function(){
			Ext.create('Ext.container.Viewport', {
			    layout: 'border',
			    frame:false,
			    border:false,
			    items: [{
			    	region: 'west',
			        split: true,
			        width: 220,
			        layout: 'fit',
			        items:[ORG_DISK.initOrgTree()]
			    },{
			    	id: 'mainPanel',
			        region: 'center',
			        layout: 'fit',
			        items:[ORG_DISK.orgUserGrid()]
			    }]
			});
		},
		initOrgTree: function(){
			//树
		 	var tree = Ext.create("Ext.com.bkfm.tree",{
		 		id: "orgTree",
		 		rootVisible: true,
		 		rootLabel: "组织管理",
		 		treeConfigId: "org",
		 		rootTreeId: "00000000000000000000000000000001",
		 		objPath: 'ROOT/XTGLPT/JCSJGL/ZZXX/BLK_BASE_ORGS',
		 		border: false,
		 		autoScroll: true,
		 		mappingField:["IS_CHILD_NODE","ORG_ID"],
		 		tbar:[{
		 			text: '新增',
		 			iconCls: 'add',
		 			handler: function(){
		 				var selOrgNode = tree.getSelectNode();
		 				if(selOrgNode.get("is_child_node") == Constants.NO){
		 					AjaxUtils.showError("所选择的组织类型为“不包含下级部门”，无法在当前组织下新增，如需新增下级部门请先修改所选择组织的属性");
		 					return;
		 				}
		 				ORG_DISK.editOrgTree();
		 			}
		 		},{
		 			text: '修改',
		 			iconCls: 'edit',
		 			handler: function(){
		 				ORG_DISK.editOrgTree(tree.getSelectNode());
		 			}
		 		},{
		 			text: '删除',
		 			iconCls: 'delete',
		 			handler: function(){
		 				tree.delSelectNode(tree.getSelectNode());
		 			}
		 		}],
		 		viewConfig:{
		            plugins :{
		                ptype:'treeviewdragdrop',
		                //appendOnly:true         //只能拖着带非叶节点上
		            },
		            listeners:{//监听器
		                drop:function(node,data,overModel,dropPosition,options){
		                    return false;
		                },
		                beforedrop:function(node,data,overModel,dropPosition,dropFunction,options){
		                	return false;
		                }
		            }
		        }
		 	});
		 	tree.on("itemclick",function(view,record,item,index,e,opts){
		 		var id = record.get("id");
		 		var title = record.get("text");
		 		var isleaf = record.get("leaf");
		 		var orgUserGrid = Ext.getCmp("orgUserGrid");
		 		orgUserGrid.search(" ORG_RWID='"+id+"'");

		 	})
		 	return tree;
		},
		editOrgTree: function(record){
			var menuTree = Ext.getCmp("orgTree");
			var win = Ext.create('Ext.com.bkfm.window', {
				title : '编辑组织',
				autoScroll : true,
				modal : true,
				height : Utils.getHeight()*0.5,
				width : Utils.getWidth()*0.5,
				items: [Ext.create('Ext.com.bkfm.form', {
					id: "editForm",
					showGroup : false,
					columnCount: 2,
					border : false,
					frameborder : false,
					objPath : "ROOT/XTGLPT/JCSJGL/ZZXX/BLK_BASE_ORGS",
					listeners:{
						afterrender: function(form,eopts){
							if(record){
								//存在表示编辑
								form.loadRemoteData(record.get("id"));
							}else{
								//新增,默认初始化上级组织
								//获取选择的组织节点
								var selOrgNode = menuTree.getSelectNode();
								form.setFieldValue(form.findById("PARENT_ORG_ID"),{
									VALUE: selOrgNode.get("id"),
									DISPLAYVALUE: selOrgNode.get("text")
								});
								//初始化组织编码
								//根节点
								var SQL = "SELECT IFNULL(MAX(ORG_ID),'') AS ORG_ID FROM BLK_BASE_ORGS WHERE PARENT_ORG_ID='"+selOrgNode.getId()+"' ";
								BkfmDBUtils.Query(SQL,function(result){
									if(result.status == "OK"){
										if(result.list !=null && result.list.length >0){
											var old_org_id = (result.list)[0]["ORG_ID"];
											if(!Ext.isEmpty(old_org_id)){
												var new_org_id = Utils.fillNumberZero(parseInt(old_org_id)+1,old_org_id.length);
												form.findById("ORG_ID").setValue(new_org_id);
												return;
											}
										}
									}
									//
									new_org_id = selOrgNode.get("org_id")+"01";
									form.findById("ORG_ID").setValue(new_org_id);
									
								});
							}
						}
					}
				})],
				tbar : [ {
					text : '保存',
					iconCls : 'save',
					handler : function() {
						var form = Ext.getCmp("editForm");
						if(form.getFormState() == Constants.RECORD_NEW){
							//新增判断菜单编码是否唯一
							var count = BkfmDBUtils.CheckNumber({
								objPath: "ROOT/XTGLPT/JCSJGL/ZZXX/BLK_BASE_ORGS",
								filter: " ORG_ID ='"+form.findById("ORG_ID").getValue()+"'"
							});
							if(Ext.isEmpty(count) || count >0){
								AjaxUtils.showError("当前组织编码已经存在");
								return;
							}
						}
						form.save(function(){
							menuTree.reloadSelectNode();
							win.close();
						});

					}
				} ]
			});
			win.show();
		},
		orgUserGrid: function(){
			var grid = Ext.create('Ext.com.bkfm.datagrid', {
				id: "orgUserGrid",
				objPath : "ROOT/XTGLPT/JCSJGL/ZZXX/BLK_ORG_USERS",
				border: 0,
				frame: false,
				orderby : ' ORDER BY USER_NO ASC',
				afterFormShow: function(gridPanel,formTabPanel){
					var form = formTabPanel.items.get(0);
					var menuTree = Ext.getCmp("orgTree");
					var selOrgNode = menuTree.getSelectNode();
					form.setFieldValue(form.findById("ORG_RWID"),{
						VALUE: selOrgNode.get("id"),
						DISPLAYVALUE: selOrgNode.get("text")
					});
				}
			});
			return grid;
		}
}